package com.hailou.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.hailou.util.DateTimeUtile;
/**
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class UserDao extends ConnectionDB{

	/**
	 * 判断用户是否第一次进入
	 * @return
	 */
	public static  boolean isNewUser(String userId){
		ConnectionDB db = new ConnectionDB();
		boolean flag = false;
		String sql = "select * from t_user t where userid = ?";
		List<Object> list = db.excuteQuery(sql,
				new String[] {userId});
		if(list==null||list.size()==0){
			sql = "insert into t_user(userid,logtime) values ('"+userId+"','"+DateTimeUtile.getNowTime()+"')";
			int num = db.executeUpdate(sql, null);
			if(num<1){
				System.out.println("=============新用户插入失败==========");
			}else{
				System.out.println("=============新用户插入成功==========");
			}
			flag = true;
		}
		return flag;
	}
	/**
	 * 获是用户个人信息
	 * @return
	 */
	public static Map<String,String> selectUserMessage(String userId){
		ConnectionDB db = new ConnectionDB();
		Map<String, String> map = new HashMap<String,String>();
		if(!isNewUser(userId)){
			String sqlselect = "select t.flag1,t.flag2,t.logtime from t_user t where t.userid = ?";
			List<Object> list = db.excuteQuery(sqlselect,new String[] {userId});
			map = (Map<String, String>)list.get(0);
		}
		return map;
	}
	
	/**
	 * 修改表中的用户信息
	 * @param userId
	 * @return
	 */
	public static int updateUserMessage(String userid , Map<String,String> params){
		StringBuilder sb = new StringBuilder();
		if(params.size()>0){
			sb.append("update t_user o set o.");
			Set<Entry<String, String>> set = params.entrySet();
			for(Iterator<Entry<String, String>> iterator = set.iterator();iterator.hasNext();){
				Entry<String, String> mm = iterator.next();
				sb.append(mm.getKey()+"='"+mm.getValue()+"',");
			}
		}
		String sql = sb.deleteCharAt(sb.length()-1).append(" where o.userid='"+userid+"'").toString();
		System.out.println("UserDao====="+sql);
		ConnectionDB db = new ConnectionDB();
		int flag = 0;
		flag = db.executeUpdate(sql, null);
		return flag;
	}
	/**
	 * 设置标识量
	 * @param userid
	 * @param value
	 */
	public static void setFlag1Value(String userid , String value){
		Map<String,String> map = new LinkedHashMap<String,String>();
		map.put("flag1",value);
		updateUserMessage(userid,map);
	}
	/**
	 * 初始化用户的标题信息，就是将flag1与flag2值设为0，将用户设置在主菜单 下。
	 * @param userid
	 * @param value
	 */
	public static void setFlag1AndFlag2Value(String userid , String... value){
		Map<String,String> map = new LinkedHashMap<String,String>();
		map.put("flag1",value[0]);
		map.put("flag2",value[1]);
		updateUserMessage(userid,map);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String,String> map = new LinkedHashMap<String,String>();
		map.put("logtime", DateTimeUtile.getNowTime());
		map.put("flag1", "0");
		updateUserMessage("oX4BotwW0t7vxSnJlfYGk-zTDdWM",map);
		
	}
}
